/**
* This is about <code>org.shoebox.guacamole.view.containers.GuaCloud</code>.
* {@link www.hyperfiction.fr}
* @author shoe[box]
*/

package org.shoebox.guacamole.view.containers {

	import flash.display3D.Context3DVertexBufferFormat;
	import org.shoebox.guacamole.core.RenderTask;
	import org.shoebox.guacamole.materials.abstracts.AMaterial;
	import org.shoebox.guacamole.view.abstracts.GuaMesh;
	import org.shoebox.utils.logger.Logger;
	
	/**
	* org.shoebox.guacamole.view.containers.GuaCloud
	* @author shoebox
	*/
	public class GuaCloud extends GuaMesh{
		
		public var oMaterial				: AMaterial;
		public var bHasChild				: Boolean = false;
		
		protected var _indiceInc			: int = 0;
		protected var _iChildsCount		: int = 0;
		protected var _vChilds				: Vector.<GuaMesh>;
		
		// -------o constructor
		
			/**
			* Constructor of the GuaCloud class
			*
			* @public
			* @return	void
			*/
			public function GuaCloud( oMaterial : AMaterial ) : void {
				material 		= oMaterial;
				_vChilds 		= new Vector.<GuaMesh>( );
				bHasChild 		= false;
				vUVCoords 		= new Vector.<Number>( );
				vIndexs 		= new Vector.<uint>( );
				vVerticesCoords = new Vector.<Number>( );
				super( );
			}

		// -------o public
			
			/**
			* addChild function
			* @public
			* @param 
			* @return
			*/
			final public function addChild( o : GuaMesh ) : void {
				_iChildsCount = _vChilds.push( o ); 
				vVerticesCoords = new Vector.<Number>( _iChildsCount * 12 , true );
				vUVCoords 		= new Vector.<Number>( _iChildsCount * 12 , true );
				vIndexs 		= new Vector.<uint>( _iChildsCount * 6 , true );
				bHasChild = true;
			}
			
			/**
			* render function
			* @public
			* @param 
			* @return
			*/
			override public function render( oTask : RenderTask ) : void {
				
				if( !bHasChild )
					return;
				
				//
					//vIndexs.splice( 0 , int.MAX_VALUE );
					//vUVCoords.splice( 0 , int.MAX_VALUE );
					//vVerticesCoords.splice( 0 , int.MAX_VALUE );
					
				//
					
					var i : int = 0 , inc : int = 0 , mesh : GuaMesh;
					
					for( i ; i < _iChildsCount ; i++ ){
						mesh = _vChilds[ i ];
						mesh.onPreRender( );
						_merge( mesh , inc , i );	
						inc += ( mesh.vVerticesCoords.length / mesh.iBufferVerticesSize );
					}
					
					

				//
					setIndexs( vIndexs ); 
					setVertices( vVerticesCoords ); 
					setUvs( vUVCoords ); 
					super.render( oTask );
					
			}
			
		// -------o protected
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _merge( oChild : GuaMesh , inc : int , id : int ) : void {
				
				var v : Vector.<Number> = oChild.getProjectVertices( );
				var i : int = 0 , l : int = 12 , z : int ;
				
				for( i ; i < l ; i++ ){
					z = i + id * 12;
					vVerticesCoords[ z ] = v[ i ]; 
					vUVCoords[ z ] = oChild.vUVCoords[ i ];
				}
				
				i = 0;
				for( i ; i < 6 ; i++ ){
					z = i + id * 6;
					vIndexs[ z ] = oChild.vIndexs[ i ] + inc;
				}
				
			}
			
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _incrementIndexs( v : Vector.<uint> , inc : int ) : Vector.<uint> {
				
				var dz : int = 0;
				var len : int = v.length;
				for( dz ; dz < len ; dz++ ){
					v[ dz ] += inc;
				}
				
				return v;
				
			}
			
		// -------o misc

			public static function trc(...args : *) : void {
				Logger.log(GuaCloud, args);
			}
	}
}
